package com.abkj.platform.tail.controller;

import com.abkj.platform.tail.domain.JcUser;
import com.abkj.platform.tail.domain.JcUserLog;
import com.abkj.platform.tail.service.BaseService;

import Decoder.BASE64Encoder;

import java.util.Date;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;

@Controller
public class BaseController
{

  @Autowired
  BaseService baseService;

  public JcUser getSessionUser(HttpServletRequest request)
  {
    return (JcUser)getSessionOjbect(request, "ABKJ-PT-ACCOUNT");
  }

  public void setSessionUser(HttpServletRequest request, JcUser user) {
    setSessionObject(request, "ABKJ-PT-ACCOUNT", user);
    setSessionObject(request, "ABKJ-PT-ACCOUNT-TYPE", Integer.valueOf(user.getType()));
  }

  public void removeSessionUser(HttpServletRequest request) {
    request.getSession().removeAttribute("ABKJ-PT-ACCOUNT");
    request.getSession().removeAttribute("ABKJ-PT-ACCOUNT-TYPE");
    request.getSession().invalidate();
  }

  public void setSessionObject(HttpServletRequest request, String name, Object obj)
  {
    request.getSession().setAttribute(name, obj);
  }

  public Object getSessionOjbect(HttpServletRequest request, String name) {
    HttpSession session = request.getSession();
    return session.getAttribute(name);
  }

  public void createJcUserLog(HttpServletRequest request, String type, String content)
  {
    JcUser jcUser = getSessionUser(request);
    JcUserLog jcUserLog = new JcUserLog();
    jcUserLog.setUid(jcUser == null ? 0 : jcUser.getId().intValue());
    jcUserLog.setCreatetime(new Date());
    jcUserLog.setType(type);
    jcUserLog.setContent(jcUser.getUsername() + " " + content);
    this.baseService.insertObject(jcUserLog);
  }

  public String getBASE64(String s) {
    if (s == null)
      return null;
    return new BASE64Encoder().encode(s.getBytes());
  }
}